﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarCoverType : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCICT";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            DataTable dtConverageType = General.GetComboItem("CoverageType", false);
            ddlCXCoverageType.DataSource = dtConverageType;
            ddlCXCoverageType.DataTextField = dtConverageType.Columns[0].ColumnName.ToString();
            ddlCXCoverageType.DataValueField = dtConverageType.Columns[1].ColumnName.ToString();
            ddlCXCoverageType.DataBind();
            ddlCXCoverageType.Items.Insert(0, new ListItem("Select", "-1"));
            dtConverageType = null;

            if (Request.Params["CXCoverTypeId"] != null)
            {
                int _cxcovertypeid = 0;
                if (int.TryParse(Request.Params["CXCoverTypeId"].ToString(), out _cxcovertypeid))
                {
                    ddlCXCoverageType.Text = _cxcovertypeid.ToString();
                    ddlCXCoverageType_SelectedIndexChanged(this, null);
                    //ShowProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), _cxcovertypeid);
                }
            }
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            CarCoverType _carcovertype = new CarCoverType();
            CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

            _carcovertype.ProviderCoverTypeId = CheckProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCoverageType.Text));

            _carcovertype.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            _carcovertype.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

            _carcovertype.ProviderCoverTypeDescription = txtYourCoverageType.Text;
            _carcovertype.CoverRate = Convert.ToSingle(txtCoverageRate.Text);
            _carcovertype.CXCoverTypeId = Convert.ToInt32(ddlCXCoverageType.Text);
            _carcovertype.MinPremiumAmount = Convert.ToSingle(txtMinimumPremium.Text);
            _carcovertype.Status = chkStatus.Checked;

            _carcovertype.ProviderCoverTypeId = _carcovertypedb.Insert(_carcovertype);
            if (_carcovertype.ProviderCoverTypeId > 0)
            {
                lblMessage.Text = "Updated successfully.";
                Response.Redirect("ViewProviderCarCoverType.aspx");
            }
            else
            {
                lblMessage.Text = "Updating failed.";
            }
        }
    }

    protected void ShowProviderCoverType(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
    {
        CarCoverType _carcovertype = new CarCoverType();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

        _carcovertype = _carcovertypedb.GetProviderCoverType(ProviderCompanyId, CXCoverTypeId);
        if (_carcovertype != null)
        {
            txtYourCoverageType.Text = _carcovertype.ProviderCoverTypeDescription;
            txtCoverageRate.Text = _carcovertype.CoverRate.ToString();
            txtMinimumPremium.Text = _carcovertype.MinPremiumAmount.ToString();
            chkStatus.Checked = _carcovertype.Status;
        }
        else
        {
            txtYourCoverageType.Text = "";
            txtCoverageRate.Text = "";
            txtMinimumPremium.Text = "";
            chkStatus.Checked = false;
        }
    }

    protected Int64 CheckProviderCoverType(Int64 ProviderCompanyId, Int32 CXCoverTypeId)
    {
        CarCoverType _carcovertype = new CarCoverType();
        CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();
        Int64 _retValue = 0;
        _carcovertype = _carcovertypedb.GetProviderCoverType(ProviderCompanyId, CXCoverTypeId);
        if (_carcovertype != null)
        {
            _retValue = _carcovertype.ProviderCoverTypeId;
        }
        return _retValue;
    }

    protected void ddlCXCoverageType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowProviderCoverType(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCoverageType.Text));
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarCoverType.aspx");
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            //btnNew.Enabled = false;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = true;
        }
    }

}
